package cn.com.se.sharepictrue.activity;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.se.sharepictrue.R;
import cn.com.se.sharepictrue.adapter.BanUserAdapter;
import cn.com.se.sharepictrue.bean.PersonBean;
import cn.com.se.sharepictrue.bean.UserInfoBean;
import cn.com.se.sharepictrue.business.CallBack;
import cn.com.se.sharepictrue.business.NetworkResponse;
import cn.com.se.sharepictrue.business.ShareApi;
import cn.com.se.sharepictrue.quickadapter.BaseAdapterHelper;
import cn.com.se.sharepictrue.quickadapter.QuickAdapter;
import cn.com.se.sharepictrue.utils.StringUtils;
import cn.com.se.sharepictrue.utils.ToastUtils;

/**
 * 用户管理界面
 * Created by KIDNG on 2015/11/5.
 */
public class ManagerUserActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener {
    @Bind(R.id.tb_manager_user_title)
    Toolbar mTbManagerUserTitle;
    @Bind(R.id.et_manager_user_input)
    EditText mEtManagerUserInput;
    @Bind(R.id.til_manager_user_input)
    TextInputLayout mTilInput;
    @Bind(R.id.tv_manager_user_ban)
    TextView mTvManagerUserBan;
    @Bind(R.id.lv_manager_user_ban_list)
    ListView mLvManagerUserBanList;
    @Bind(R.id.srl_manager_refresh)
    SwipeRefreshLayout mSrlManagerRefresh;
    private List<UserInfoBean> mBeans;
    private BanUserAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_user);
        ButterKnife.bind(this);
        initToolbar();
        mTilInput.setHint(getResources().getString(R.string.manager_user_ban_input));
        initRefreshView();
    }

    private void initRefreshView() {
        mSrlManagerRefresh.setOnRefreshListener(this);
        mSrlManagerRefresh.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        mSrlManagerRefresh.post(new Runnable() {
            @Override
            public void run() {
                mSrlManagerRefresh.setRefreshing(true);
                initData();
            }
        });
    }

    private void initData() {
        ShareApi.BanUserList(this, new CallBack<NetworkResponse<List<UserInfoBean>>>() {
            @Override
            public void onStart() {
                super.onStart();
                mSrlManagerRefresh.setRefreshing(true);
            }

            @Override
            public void onFail() {
                mSrlManagerRefresh.setRefreshing(false);
            }

            @Override
            public void onResponse(NetworkResponse<List<UserInfoBean>> response) {
                mSrlManagerRefresh.setRefreshing(false);
                if (!response.isStatus()) {
                    ToastUtils.show(ManagerUserActivity.this, response.getError());
                } else {
                    mBeans = response.getData();
                    initList();
                }
            }
        });
    }

    private void initToolbar() {
        setSupportActionBar(mTbManagerUserTitle);
        mTbManagerUserTitle.setTitleTextColor(Color.WHITE);
    }

    private void initList() {
//        mAdapter = new QuickAdapter<PersonBean>(this, R.layout.item_person_list, mBeans) {
//            @Override
//            protected void convert(final BaseAdapterHelper helper, PersonBean item) {
//                Button concern = helper.getView(R.id.btn_person_list_concern);
//                concern.setText(getResources().getString(R.string.manager_user_cancel_ban));
//                helper.setText(R.id.tv_person_list_name, item.name);
//                helper.setText(R.id.tv_person_list_introduction, item.introduction);
//            }
//        };
        mAdapter = new BanUserAdapter(this, R.layout.item_person_list, mBeans);
        mAdapter.setOnBanListener(new BanUserAdapter.OnBanListener() {
            @Override
            public void onBan(BaseAdapterHelper helper, final UserInfoBean item) {
                ShareApi.adminBanUser(ManagerUserActivity.this,
                        item.getIsAllow().equals(UserInfoBean.IS_ALLOW),
                        item.getUserID(),
                        new CallBack<NetworkResponse<String>>(ManagerUserActivity.this, "正在操作...") {
                            @Override
                            public void onFail() {
                                ToastUtils.show(ManagerUserActivity.this, "网络访问失败咯");
                            }

                            @Override
                            public void onResponse(NetworkResponse<String> response) {
                                if (!response.isStatus()) {
                                    ToastUtils.show(ManagerUserActivity.this,
                                            StringUtils.isEmpty(response.getError()) ? "" : response.getError());
                                } else {
                                    if (item.getIsAllow().equals(UserInfoBean.NO_ALLOW))
                                        item.setIsAllow(UserInfoBean.IS_ALLOW);
                                    else
                                        item.setIsAllow(UserInfoBean.NO_ALLOW);
                                }
                                mAdapter.notifyDataSetChanged();
                            }
                        });
            }
        });
        mLvManagerUserBanList.setAdapter(mAdapter);

    }

    @OnClick(R.id.tv_manager_user_ban)
    public void toBan(View v) {
        String userID = mTilInput.getEditText().getText().toString();
        toBanUser(userID, true);
    }

    private void toBanUser(String userID, boolean toBan) {
        ShareApi.adminBanUser(this, toBan, userID, new CallBack<NetworkResponse<String>>(ManagerUserActivity.this, "正在禁止...") {
            @Override
            public void onFail() {
                ToastUtils.show(ManagerUserActivity.this, "网络访问失败咯");
            }

            @Override
            public void onResponse(NetworkResponse<String> response) {
                if (!response.isStatus()) {
                    ToastUtils.show(ManagerUserActivity.this,
                            StringUtils.isEmpty(response.getError()) ? "" : response.getError());
                } else {
                    ToastUtils.show(ManagerUserActivity.this, "禁止成功");
                    initData();
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onRefresh() {
        initData();
    }
}
